package cn.thoughtworks.factory;

import cn.thoughtworks.model.Maze;
import cn.thoughtworks.model.Size;

/**
 * 迷宫工厂
 *
 * @author briarbear
 * @blog https://briarbear.github.io
 * @create 2018-07-27-15:18
 */
public class MazeFactory implements Factory {

    /**
     * 迷宫工厂返回迷宫类
     * @param commond 命令
     * @return 迷宫类
     */
    public Maze create(Size size,String commond) {

        int row = size.getRow()*2+1;
        int col = size.getCol()*2+1;
        Maze maze = new Maze(row,col);
        int[][] roads = initMaze(row,col,commond);
        maze.setRoads(roads);
        return maze;
    }

    /**
     * 根据初始化迷宫
     * @param row 行数
     * @param col 列数
     * @param commond 命令
     * @return 二维数组
     */
    private int[][] initMaze(int row, int col, String commond) {
        int[][] roads = new int[row][col];
        init((row-1)/2,(col-1)/2,roads);

        String[] arr1 = commond.split(";");
        for (int i = 0; i < arr1.length; i++) {
            String[] arr2 = arr1[i].split(" ");
            int[] n1 = new int[2];
            int[] n2 = new int[2];
            n1[0] = Integer.valueOf(arr2[0].split(",")[0]);
            n1[1] = Integer.valueOf(arr2[0].split(",")[1]);
            n2[0] = Integer.valueOf(arr2[1].split(",")[0]);
            n2[1] = Integer.valueOf(arr2[1].split(",")[1]);

            if (n1[0]==n2[0]){
                for (int j = n1[1]*2+1; j <=n2[1]*2+1 ; j++) {
                    roads[n1[0]*2+1][j] = 1 ;
                }
            }else {
                for (int j = n1[0]*2+1; j <=n2[0]*2+1 ; j++)
                    roads[j][n1[1]*2+1] = 1 ;
            }

        }


        return roads;
    }

    /**
     * 初始化迷宫
     * @param size
     */
    private void init(int row,int col,int[][] roads){

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                roads[i*2+1][j*2+1] = 1;
            }
        }
    }

}
